<?php
global $Slickr;?>

<?php if ($error):?>
<div id="message" class="error fade"><p><strong><?php echo $error?></strong></p></div>
<?php elseif ($message):?>
<div id="message" class="updated fade"><p><strong><?php echo $message?></strong></p></div>
<?php endif;?>

<div class="wrap">
<h2>Slickr Gallery Options</h2>
<?php if (!is_object($Slickr)):?>
    <?php if (get_option('Slickr_flickr_nag') == 1):?>
        <!-- I hate doing this... -->
        <?php insert_paypal_nag();?>
    <?php endif;?>
<?php endif;?>

<p>This plugin will retrieve your Flickr photosets and display them as albums on a page within this site. It also allows you to easily add your photos to your blog posts or pages. To insert photos into your posts, just click the <em>Photos</em> tab while <a href="<?=get_settings('siteurl')?>/wp-admin/post.php" title="create a new post">editing a post</a> and select a thumbnail.</p>

<p><strong>Before you start:</strong></p> 
<ul>
<li>Slickr requires a working <a href="http://www.stimuli.ca/lightbox" title="get Lightbox">Lightbox plugin</a> to function properly.</li>
<li>After you have updated a Flickr set, or created a new one, visit this page to refresh Slickr&apos;s cache to show your latest photos.</li>
<li>This plugin is provided &apos;as is&apos;, meaning it is <em>unsupported</em> and comes with no official technical support. <br />For unofficial technical support, as well as updated versions of Slickr, visit <a href="http://www.stimuli.ca/slickr">www.stimuli.ca</a>. You can subscribe to <a rel="alternate" type="application/rss+xml" title="my RSS feed" href="http://www.stimuli.ca/feed/">my RSS feeds</a> to be notified of new versions.</li>
</ul>

<?php if (!$flickrAuth):?>
<?php
    $redirect = $_SERVER['SCRIPT_URI'] . '?' . $_SERVER['QUERY_STRING'];
    $perms = "read";
    $api_sig = md5($flickr->getSharedSecret() . 
        "api_key" . $flickr->getAPIKey() . 
        "frob"  . $frob .
        "perms" . $perms);
?>

<h3>Flickr Settings</h3>

<p>Slickr is currently not linked to your Flickr account. Let&apos;s get started!</p>
<h3>Step 1:</h3>
<p>Login to Flickr and grant <em>read only</em> permissions to this photo album.
Once you are done, close the popup window and click the button in Step 2.
</p>

<form method="get" action="http://flickr.com/services/auth/" target="_blank">
<input type="hidden" name="api_key" value="<?php echo $flickr->getAPIKey()?>" />
<input type="hidden" name="frob" value="<?php echo $frob?>" />
<input type="hidden" name="perms" value="<?php echo $perms?>" />
<input type="hidden" name="api_sig" value="<?php echo $api_sig?>" />
<input type="submit" value="Retrieve Flickr Permissions &raquo;" />
</form>

<h3>Step 2:</h3>    
<p>Apply the permissions granted in Step 1 to this photo album. This step may take a minute to complete, since it&apos;s also going to pull down your Flickr information.</p>
    
<form method="post" id="flickr" action="">
<input type="hidden" name="action" value="save" />
<input type="hidden" name="frob" value="<?php echo $frob?>" />
<input type="submit" value="Apply Permissions &raquo;" />
</form>

<p>
<strong>Note:</strong> You can revoke the permissions granted here in <a href="http://flickr.com/services/auth/list.gne">your Flickr access control panel</a>.
</p>


<?php else:?>

<fieldset class="options"> 
<legend>Gallery Settings</legend>

<table class="optiontable">
<tr valign="baseline">
<th scope="row">Cached Recent Photos:</th> 
<td>
<?php global $SlickrPlugin; $myphotos = $SlickrPlugin->getSlickrRecentPhotos();?>
<?php foreach ($myphotos as $photo):?>
        <img src="<?php echo $photo['sizes']['Square']['source']?>" width="<?php echo $photo['sizes']['Square']['width']/2?>" height="<?php echo $photo['sizes']['Square']['height']/2?>" alt="<?php echo $photo['title']?>" style="border: 1px solid #000;"/>
<?php endforeach;?>
<p><small>Your Flickr albums are cached locally to speed things up. Refresh your albums to show more recent additions.</small></p>
<form method="post" action="">
<input type="hidden" name="action" value="clearcache" />
<input type="hidden" name="album" value="all" />
<p class="submit" style="float:left;"><input type="submit" value="Refresh Albums &raquo;" /></p>
</form>
</td>
</tr>
</table>


<table class="optiontable">
<tr valign="baseline">
<th scope="row">Flickr Account:</th> 
<td>
<form method="post" action="">
<input type="hidden" name="action" value="logout" />
<a href="http://flickr.com/photos/<?php echo $user['user']['nsid']?>/"><strong><?php echo $user['user']['username']?></strong></a> <input type="submit" class="submit" value="Remove Link &raquo;" /></form> <p><small>Slickr Gallery&apos;s access is <em>read only</em></small></p>
</td>
</tr>
</table>

<form method="post" action="">
<input type="hidden" name="action" value="galleryoptions" />
<p class="submit"><input type="submit" value="save settings &raquo;" /></p>
<table class="optiontable">
<tr valign="baseline">
<th scope="row">Slickr Gallery URL:</th> 
<td>
<? bloginfo('siteurl')?>/
<input type="text" name="baseurl" value="<?php echo substr($baseurl, strlen($baseurl_pre))?>" />

<p><small>Enter the path where you want your photo album to be shown. 
    <?php if ($baseurl):?>
    View your album: <a href="<?php echo $baseurl?>"><?php echo $baseurl?></a>
    <?php endif;?>
    </small>
</p>
</td>
</tr>

<tr valign="baseline">
<th scope="row">Show Albums:</th> 
<td>
<?php 
/* Our Album selection checkboxes: */
$albums = $flickr->getAlbums();
$showme = get_option('Slickr_showalbum');
//print_r($albums);
//print_r($showme);

foreach($albums as $album) {
    echo("<input type=\"checkbox\" name=\"".$album['id']."\" value=\"1\" ");
    if(($showme[$album['id']]) == 1) {
        echo("checked=\"checked\" ");
    }
    echo("/> ".$album['title']."<br /><br />");
}
?>
<p><small>De-select albums you don&apos;t want to show</small></p>
</td>
</tr>

<tr valign="baseline">
<th scope="row">Albums Per Page:</th> 
<td>
<input type="text" name="Slickr_flickr_max_albums" maxlength="2" size="2" value="<?php echo(get_option('Slickr_flickr_max_albums'))?>" />
<p><small>Number of Flickr albums to show at once</small></p>
</td>
</tr>

<tr valign="baseline">
<th scope="row">Photos Per Page:</th> 
<td>
<input type="text" name="Slickr_flickr_max_photos" maxlength="2" size="2" value="<?php echo(get_option('Slickr_flickr_max_photos'))?>" />
<p><small>Number of Flickr photo thumbnails to show at once</small></p>
</td>
</tr>

<tr valign="baseline">
<th scope="row">Slickr Gallery Theme:</th> 
<td>
<?php
/* Check if there are themes: */
$Slickr_themes_path =  get_option('Slickr_themes_path');
//print_r($Slickr_themes_path);
if ($handle = opendir($Slickr_themes_path)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && $file != ".DS_Store") {
            $theme_dirs[$file] = $Slickr_themes_path."/".$file."/";
        }   
    }
    closedir($handle);
}
//print_r($theme_dirs);
/* Create a drop-down menu of the valid themes: */
echo("\n<select name=\"Slickr_theme\">\n");
$current_theme = get_option('Slickr_theme');
foreach($theme_dirs as $shortname => $fullpath) {
    if((file_exists($fullpath."/slickr.css")) && (file_exists($fullpath."/view.php"))) {
        if($current_theme == urlencode($shortname)) {
            echo("<option value=\"".urlencode($shortname)."\" selected=\"selected\">".$shortname."</option>\n");
        } else {
            echo("<option value=\"".urlencode($shortname)."\">".$shortname."</option>\n");
  
        }
    }
}
echo("</select>\n");

?>
<p><small>If in doubt, try the <em>Default</em> theme</small></p>
</td>
</tr>

<tr valign="baseline">
<th scope="row">Sidebar Widget Options:</th> 
<td>
<?php if (function_exists('register_sidebar_widget')):?>
<input type="checkbox" name="showbadge" value="1" <?php echo ($showbadge ? 'checked="checked"' : '')?> id="showbadge" />
<label for="showbadge"> Enable the Flickr Widget for your sidebar.</label>
<br />
<small>This will show your recent photos in your site&apos;s sidebar.</small>
<?php
global $registered_sidebars;
if (count($registered_sidebars) <= 0):?>
<br /><strong>Note:</strong> Your theme is not configured for WordPress Widgets. Here are instructions on <a href="http://automattic.com/code/widgets/themes/">how to add WordPress Widgets</a> to your existing theme.
<?php else:?>
<small><a href="themes.php?page=widgets/widgets.php">Configure your sidebar &gt;</a></small>
<?php endif;?>
<?php else:?>
Install the <a href="http://automattic.com/code/widgets/">WordPress Widgets plugin</a> if you want to show your recent Flickr photos in your WordPress sidebar.</small>
<?php endif;?>
</td>
</tr>


<tr valign="baseline">
<th scope="row">Hemingway Block Options:</th> 
<td>
Show 
<input type="text" name="Slickr_widget_number_of_photos" maxlength="2" size="2" value="<?php echo(get_option('Slickr_widget_number_of_photos'))?>" /> recent photos in the block. Randomize the photos:
<input type="checkbox" name="Slickr_widget_randomize" value="1" <?php echo ($Slickr_widget_randomize ? 'checked="checked"' : '')?> id="randomize" />
</td>
</tr>

</table>

<p class="submit"><input type="submit" value="save settings &raquo;" /></p>

</form>
</fieldset>

<?php endif;?>
</div>
